<?php

class m_users_documents_UserBase extends f_document_BaseDocument
{

	private $login;
	private $password;
	private $firstname;
	private $lastname;
	private $email;
	private $rights = array();
	private $extraParams;
	private $backLang;

	public function __construct($id = null, $lang = null)
	{
		parent::__construct($id, null, $lang);
	}

	public function getTableName()
	{
		return "m_users__user";
	}

	public function getDocumentModel()
	{
		return "modules_users/user";
	}
	
	protected function getFieldsBag()
	{
		return array('document_id', 			'document_uid', 			'document_model', 			'document_author', 			'document_creationdate', 			'document_modificationdate', 			'document_publicationstatus', 			'document_lang', 			'document_revision', 			'document_startpublicationdate', 			'document_endpublicationdate', 			'document_label', 			'login', 			'password', 			'firstname', 			'lastname', 			'email', 			'rights', 			'extraParams', 			'backLang');
	}

	public function getdefaultPublicationstatus()
	{
		return "PUBLISHED";
	}

	/**
	 * @return boolean
	 */
	public function isRevisable()
	{
		return false;
	}
	
	/**
	 * @return boolean
	 */
	public function isLocalized()
	{
		return false;
	}	
	
    /**
	 * @return boolean
	 */
	public function isRecursive()
	{
		return $this->isRecursive;
	}
	
	/**
	 * @param boolean $value
	 * @return m_users_documents_UserBase
	 */	
	public function setIsRecursive($value)
	{
		$this->isRecursive = $value;
	}
		
	/**
	 * @return string
	 */
	public function getLogin()
	{
		return $this->login;
	}

	/**
	 * @param string $value
	 * @return m_users_documents_User
	 */
	public function setLogin($value)
	{
		$this->login = $value;
		return $this;
	}
	/**
	 * @return string
	 */
	public function getPassword()
	{
		return $this->password;
	}

	/**
	 * @param string $value
	 * @return m_users_documents_User
	 */
	public function setPassword($value)
	{
		$this->password = $value;
		return $this;
	}
	/**
	 * @return string
	 */
	public function getFirstname()
	{
		return $this->firstname;
	}

	/**
	 * @param string $value
	 * @return m_users_documents_User
	 */
	public function setFirstname($value)
	{
		$this->firstname = $value;
		return $this;
	}
	/**
	 * @return string
	 */
	public function getLastname()
	{
		return $this->lastname;
	}

	/**
	 * @param string $value
	 * @return m_users_documents_User
	 */
	public function setLastname($value)
	{
		$this->lastname = $value;
		return $this;
	}
	/**
	 * @return string
	 */
	public function getEmail()
	{
		return $this->email;
	}

	/**
	 * @param string $value
	 * @return m_users_documents_User
	 */
	public function setEmail($value)
	{
		$this->email = $value;
		return $this;
	}
	/**
	 * Add a new related document by document_uid	 
	 * 
	 * @return m_users_documents_User
	 */
	public function addRightsById($documentId, $orderNumber = null)
	{
		$document = f_document_Provider::getInstance()->getByUniqueId($documentId);
		return $this->addRights($document, $orderNumber);
	}

	/**
	 * Add a new related document
	 * 
	 * @return m_users_documents_User
	 */
	public function addRights($document, $orderNumber = null)
	{		
		if(is_object($document) && $document instanceof m_rights_documents_Rights)
		{
			if(!is_array($this->rights))
			{
				$this->rights = array();
			}
		
			if(!in_array($document, $this->rights))
			{
				if(is_null($orderNumber))
				{
					$this->rights[] = $document;
				}
				else
				{
					$this->rights[$orderNumber] = $document;
				}
			}
		}
		else
		{
			throw new Exception("Document is not a valid object or from valid model(m_rights_documents_Rights)");	
		}
		return $this;
	}
	
	/**
	 * Get the related documents in array
	 *
	 * @return array m_rights_documents_Rights
	 */
	public function getRights()
	{
		$relationManager = f_relation_Manager::getInstance();
		return $relationManager->getChilds($this, "rights", $this->getLang());
	}
	
	/**
	 * Get related documents number
	 * 
	 * @return integer
	 */
	public function getRightsNb()
	{
		return $this->rights;
	}		
	
	/**
	 * Set the number of related documents
	 * Only used when saving or initialize document
	 */
	protected function setRights($value)
	{
		if(!is_null($value) && is_int($value))
		{
			$this->rights = $value;
		}
		else
		{
			$dbProvider = f_database_Provider::getInstance();
	
			$sql = "SELECT COUNT(`child_id`) FROM `f_relation` WHERE `relation_type`=:relationType AND `lang`=:lang AND `parent_id`=:parentId";
			
			$nbRelations = $dbProvider->setQuery($sql)->execute(array("relationType" => 'rights', "lang" => $this->getLang(), "parentId" => $this->getUniqueId()))->getQueryResult(PDO::FETCH_COLUMN);
	
			$this->rights = intval($nbRelations);
		}
	}
	/**
	 * @return string
	 */
	public function getExtraParams()
	{
		return $this->extraParams;
	}

	/**
	 * @param string $value
	 * @return m_users_documents_User
	 */
	public function setExtraParams($value)
	{
		$this->extraParams = $value;
		return $this;
	}
	/**
	 * @return string
	 */
	public function getBackLang()
	{
		return $this->backLang;
	}

	/**
	 * @param string $value
	 * @return m_users_documents_User
	 */
	public function setBackLang($value)
	{
		$this->backLang = $value;
		return $this;
	}
	public function relationFields()
	{
		return array_merge(array('rights'),parent::relationFields());
	}



}